package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.RecordCourseCheck;
import com.xmy.cultivate.entity.RecordCourseLack;
import com.xmy.cultivate.enums.StatusRecordEnum;
import com.xmy.cultivate.mapper.RecordCourseCheckMapper;
import com.xmy.cultivate.service.IRecordCourseCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IRecordCourseLackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课时核对 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-04
 */
@Service
public class RecordCourseCheckServiceImpl extends ServiceImpl<RecordCourseCheckMapper, RecordCourseCheck> implements IRecordCourseCheckService {

    @Autowired
    RecordCourseCheckMapper recordCourseCheckMapper;

    @Autowired
    IRecordCourseLackService iRecordCourseLackService;

    @Override
    public RecordCourseCheck getResidueCount(Integer yearPart, Integer quarterNum, Long studentId, Long subjectsId) {
        return recordCourseCheckMapper.getResidueCount(yearPart,quarterNum,studentId,subjectsId);
    }

    @Override
    public Integer getResidueCourseCount(Long recordCourseId) {
        return recordCourseCheckMapper.getResidueCourseCount(recordCourseId);
    }

    @Override
    public boolean addData(RecordCourse recordCourse){
        RecordCourseCheck getResidueCount = this.getResidueCount(recordCourse.getYearPart(), recordCourse.getQuarter(), recordCourse.getStudentId(), recordCourse.getSubjectsId());

        RecordCourseCheck recordCourseCheck = recordCourseCheckMapper.getOneForRecordCourseId(recordCourse.getId());
        if (recordCourseCheck == null){
            recordCourseCheck = new RecordCourseCheck();
        }
        /**
         * 请假并已经补课的
         */
        if (recordCourse.getStatus().equals(StatusRecordEnum.LEAVE) && recordCourse.getIsRepair().equals(1)){
            RecordCourseLack recordCourseLack = iRecordCourseLackService.getById(recordCourse.getId());
            if (recordCourseLack != null){
                recordCourseCheck.setBuckleClassHouse(recordCourseLack.getBuckleClassHouse());
            }
        }else {
            recordCourseCheck.setBuckleClassHouse(recordCourse.getBuckleClassHouse());
        }
        recordCourseCheck.setRecordCourseId(recordCourse.getId());
        recordCourseCheck.setBuckleClassMoney(recordCourse.getBuckleClassMoney());
        if (getResidueCount != null) {
            recordCourseCheck.setResidueCourseCount(getResidueCount.getResidueCourseCount());
            recordCourseCheck.setResiduePrice(getResidueCount.getResiduePrice());
        }
        this.saveOrUpdate(recordCourseCheck);
        return true;
    }
}
